package com.bsh.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.mybatis.model.Partner;
import com.bsh.mybatis.model.PartnerUpgradeAsk;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoPartner;
import com.bsh.repository.DaoPartnerUpgradeAsk;
import com.bsh.service.ApiPartnerService;
import com.bsh.service.PartnerService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Des3;

@Service
@Transactional
public class ApiPartnerServiceImpl implements ApiPartnerService {

	@Autowired
	DaoPartner partnerDao;
	@Autowired
	PartnerService ps;
	@Autowired
	DaoPartnerUpgradeAsk  partnerAskDao;
	@Autowired
	UserService servUser;
	

	/**
	 * 添加合伙人
	 * 
	 * @param dto
	 * @return
	 * @throws ApiException
	 */
	@Override
	public ApiMessage savePartner(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
		if (ApiUtils.objIsEmpty(dto.getString("userName"))) {
			throw new ApiException("105", "缺少用户名");
		}
		if (ApiUtils.objIsEmpty(dto.getString("token"))) {
			throw new ApiException("103", "缺少token");
		}
		if (ApiUtils.objIsEmpty(dto.getInteger("partnerLevel"))) {
			throw new ApiException("103", "缺少合伙人等级");
		} else if (dto.getInteger("partnerLevel") > 4 || dto.getInteger("partnerLevel") < 1) {
			throw new ApiException("103", "合伙人等级不存在");
		}

		if (ApiUtils.objIsEmpty(dto.getInteger("province"))) {
			throw new ApiException("103", "缺少省");
		}
		if (ApiUtils.objIsEmpty(dto.getInteger("city"))) {
			throw new ApiException("103", "缺少市");
		}
		// if (ApiUtils.objIsEmpty(dto.getInteger("area"))) {
		// msg = new ApiMessage("103", "缺少区县");
		// return msg;
		// }
		User user = servUser.getUserByName(dto.getString("userName"));
		if (ApiUtils.objIsEmpty(user)) {
			throw new ApiException("102", "用户不存在");
		}
		List<Map<String, Object>> partnerInfoList = partnerDao.getUpgradePartnerByUserid(user.getId(),-1);
		if (partnerInfoList.size() > 0) {
			throw new ApiException("102", "合伙人信息已提交");
		}
		
		
		// 处理结果，添加数据
		Partner partnerReq = new Partner();
		partnerReq.setUserid(user.getId());
		partnerReq.setPartnerLevel(dto.getInteger("partnerLevel"));
		partnerReq.setCountry(1);// 1 中国
		partnerReq.setProvince(dto.getInteger("province"));
		partnerReq.setCity(dto.getInteger("city"));
		partnerReq.setArea(dto.getInteger("area"));
		partnerReq.setRealName(user.getRealName());
		partnerReq.setStatus((byte) 0);
		partnerReq.setCreateTime(new Date());
		Partner partner1 = ps.addPartner(partnerReq);
		if (partner1 != null) {
			String msgTitle = "添加合伙人请求成功!";
			msg = new ApiMessage("200", msgTitle);
		} else {
			msg = new ApiMessage("400", "添加合伙人请求失败!");
		}

		return msg;
	}

	/**
	 * 查询合伙人申请记录最新一条
	 * 
	 * @param dto
	 * @return
	 * @throws ApiException
	 */
	@Override
	public ApiMessage checkPartner(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
		if (ApiUtils.objIsEmpty(dto.getString("userName"))) {
			throw new ApiException("105", "缺少用户名");
		}
		if (ApiUtils.objIsEmpty(dto.getString("token"))) {
			throw new ApiException("103", "缺少token");
		}
		User user = servUser.getUserByName(dto.getString("userName"));
		if (ApiUtils.objIsEmpty(user)) {
			throw new ApiException("102", "用户不存在");
		}
		Map<String, Object> partnerInfo = null;
		List<Map<String, Object>> list = ps.getPartnerByUserid(user.getId());
		if (list.size() > 0) {
			partnerInfo = list.get(0);
		}else{
			throw new ApiException("102", "合伙人信息不存在");
		}
		msg = new ApiMessage("200", "查询成功");
		JSONObject data = new JSONObject();
		data.put("partnerInfo", partnerInfo);
		msg.setData(Des3.encResponse(data));
		return msg;
	}

	/**
	 * 升级合伙人
	 * 
	 * @param dto
	 * @return
	 * @throws ApiException
	 */
	@Override
	public ApiMessage upgradePartner(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
		if (ApiUtils.objIsEmpty(dto.getString("userName"))) {
			throw new ApiException("105", "缺少用户名");
		}
		if (ApiUtils.objIsEmpty(dto.getString("token"))) {
			throw new ApiException("103", "缺少token");
		}
		if (ApiUtils.objIsEmpty(dto.getInteger("partnerLevel"))) {
			throw new ApiException("103", "缺少升级等级");
		}
		User user = servUser.getUserByName(dto.getString("userName"));
		if (ApiUtils.objIsEmpty(user)) {
			throw new ApiException("102", "用户不存在");
		}
		// 先查询出最后一条合伙人信息
		Map<String, Object> partnerInfo = null;
		List<Map<String, Object>> partnerInfoList = partnerDao.getUpgradePartnerByUserid(user.getId(),1);
		if (partnerInfoList.size() > 0) {
			partnerInfo = partnerInfoList.get(0);
		} else {
			throw new ApiException("102", "合伙人不存在");
		}
		Integer id = (Integer) partnerInfo.get("id");
		Partner partner = ps.getPartner(id);
		if (partner.getStatus().equals((byte) 0)) {
			throw new ApiException("102", "合伙人未审批通过");
		}
		if (dto.getShort("partnerLevel")>=partner.getPartnerLevel() || dto.getShort("partnerLevel") < 1
				|| dto.getInteger("partnerLevel") > 3) {
			throw new ApiException("102", "合伙人等级不合法");
		}
		//查询合伙人升级信息
		List<Map<String, Object>> partnerAskInfoList = partnerAskDao.getUpgradePartnerAskByPartnerId(partner.getId(),0);
		if (partnerAskInfoList.size() > 0) {
			throw new ApiException("102", "合伙人升级正在审批中");
		}
		//保存升级记录
		PartnerUpgradeAsk partnerAsk=new PartnerUpgradeAsk();
		partnerAsk.setPartnerId(id);
		partnerAsk.setUpLevel(dto.getInteger("partnerLevel"));
		partnerAsk.setLevel(partner.getPartnerLevel());
		partnerAsk.setAuditStatus((short) 0);
		partnerAsk.setCreateTime(new Date());
		Integer res=partnerAskDao.insert(partnerAsk);
		if(res>0){
			msg = new ApiMessage("200", "升级合伙人申请成功");
		}else{
			msg = new ApiMessage("400", "升级合伙人申请失败");
		}
		return msg;
	}

}
